<template>
  <div>
    <!-- 搜索 -->
    <!-- 主损件故障代码关系维护 -->
    <TopFilter :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="110" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :dataSource="dataList"
      :datakey="'records'"
      :fetchapi="fetchapi"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
    >
        <template slot="controls" slot-scope="props">
          <multiuse-button size="small" type="primary" icon="el-icon-check" :click="qiyong.bind()" :disabled="disableds">启用</multiuse-button>
          <multiuse-button size="small" type="primary" icon="el-icon-close" :click="tingyong.bind()":disabled="disableds">禁用</multiuse-button>
          <multiuse-button size="small" type="primary" icon="el-icon-plus" @click="add()">新增</multiuse-button>
          <multiuse-button type="primary" :click="exportFileBtn.bind()">导出</multiuse-button>
          <multiuse-button size="small" type="primary" :click="downloadTemplate.bind()">下载模板</multiuse-button>
          <multiuse-button size="small" icon="el-icon-upload" @click="showImportFun()" >导入</multiuse-button>
        </template>
    </FilterTable>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
    </div>
      <BaseDialog :visible.sync="zhusunjian" :title="'主损件选择'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
        <Selectzhusunjian @close="zhusunjianClose" @save="zhusunjianSave"></Selectzhusunjian>
      </BaseDialog>
      <BaseDialog :visible.sync="guzhangxianxiangdaima" :title="'故障现象选择'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
        <Selectguzhangxianxiang @close="guzhangxianxiangdaimanClose" @save="guzhangxianxiangdaimaSave"></Selectguzhangxianxiang>
      </BaseDialog>
      <BaseDialog :visible.sync="visibleAdd" :title="'主损件故障代码关系新增'" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
        <AddForm @close="closeAdd" @save="saveAdd"></AddForm>
      </BaseDialog>
      <BaseDialog :visible.sync="emitVisible" title="主损件故障代码关系编辑" width="920" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
        <editForm @close="emitCloseHandle" @save="emitChange" :rowData="rowData"/>
      </BaseDialog>  
      <BaseDialog :visible.sync="showImport" :closable="false" icon="el-icon-upload" title="导入" width="800px" destroyOnClose :containerStyle="{ height: 'calc(100% - 120px)', overflow: 'auto', paddingBottom: '60px' }">
      <importExcel @close="showImport = false" @importclose="editsave()" url="/api/cms_claimfac/rPFapa/importExcel"/>
      </BaseDialog>
  </div>
</template>
<script>
  import {getListByPage,getListForExport,downloadModel, piliangqiyong, piliangtingyong,deleteOne,exporturl} from '@/api/claim/factory/PartAndFault';
  import Selectzhusunjian from '@/pages/claimDemage/tools/vehicle.vue';
  import Selectguzhangxianxiang from './Selectguzhangxianxiang.vue';
  import AddForm from './AddForm.vue';
  import editForm from './editForm.vue';
  import { exportExcelFile } from '@/utils/index';
  import importExcel from '@/pages/claimDemage/tools/importExcel.vue';
export default {
  // mixins: [dictionary],
    components: {
    Selectzhusunjian,
    AddForm,
    editForm,
    Selectguzhangxianxiang,
    importExcel
  },
  data() {
    return {
      dataList: [],
      visible: false,
      disableds: true,
      fetchapi:getListByPage,
      visibleDrawer: false,
      collapse: false,
      zhusunjian: false,
      emitVisible: false,
      rowData: {},
      guzhangxianxiangdaima: false,
      infoId: null,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      showImport: false,
      columns: this.creatHeader(),
      // fetchApi: getUser,
      fetchParams: {
          xhrAbort: true
      }, //ipa 接口
      visibleAdd: false,
      selectedRows: [] //选中经销商
    };
  },
  created() {
  },
  methods: {
    // 删除
    async delFun(data) {
      this.$confirm('确定删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          console.log(data.fapaId)
          this.res = await deleteOne({fapaId: data.fapaId});
          if (this.res.resultCode != 200) {
            this.$notify.error({ title: '提示信息', message: res.errMsg || '删除失败!' });
          } else {
            this.$notify.success({ title: '提示信息', message: '删除成功!' });
            // 重新加载
            this.fetchParams = { ...this.fetchParams };
          }
        })
    },
    //批量启用
    async qiyong() {
      if (this.selectedRows.length > 0) {
        let str = '';
        this.selectedRows.map(x => {
          str += x.fapaId + ',';
        });
        console.log(str);
        const res = await piliangqiyong({ids: str, status: '1'});
        this.fetchParams = {...this.fetchParams};
      } else {
        return this.$notify.warning({title: '提示信息', message: '请勾选后再进行启用！'});
      }
    },
    //批量停用
    async tingyong() {
      if (this.selectedRows.length > 0) {
        let str = '';
        this.selectedRows.map(x => {
          str += x.fapaId + ',';
        });
        console.log(str);
        const res = await piliangtingyong({ids: str, status: '0'});
        this.fetchParams = {...this.fetchParams};
      } else {
        return this.$notify.warning({title: '提示信息', message: '请勾选后再进行禁用！'});
      }
    },
    editsave() {
      this.showImport = false;
      this.fetchParams = { ...this.fetchParams };
    },
    // Excel更新导入
    showImportFun() {
      this.showImport = true;
    },
    //下载模板
    downloadTemplate() {
      this.$confirm('此操作将下载excel模板, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.exportLoading = true;
        downloadModel().then(res => {
          console.log(res);
          exportExcelFile(res.data, '主损件故障代码维护导入模板');
          this.exportLoading = false;
        });
      });
    },
   async  exportFileBtn() {
      let json = Object.assign({}, { CODE: 'CODE_00000129' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
    },
    emitFun(data) {
      this.rowData = data;
      this.emitVisible = true;
    },
    emitCloseHandle() {
      this.emitVisible = false;
    },
    emitChange(val) {
      this.emitVisible = false;
      this.fetchParams = { ...this.fetchParams };
    },
    //新增
    add() {
      this.visibleAdd = true;
    },
    closeAdd() {
      this.visibleAdd = false;
    },
    saveAdd() {
      this.visibleAdd = false;
      this.fetchParams = { ...this.fetchParams };
    },
    //故障现象
    guzhangxianxiangdaimanClose() {
      this.guzhangxianxiangdaima = false;
    },
    guzhangxianxiangdaimaSave(obj) {
      this.faultId = obj[0].faultId;
      this.topFilterInit.map(x =>{
        if ('faultCode' == x.fieldName) {
            x.initialValue = obj[0].faultCode; 
        }
        if ('faultName' == x.fieldName) {
            x.initialValue = obj[0].faultName; 
        }
        if ('sysCode1' == x.fieldName) {
            x.initialValue = obj[0].sysCode1; 
        }
        if ('sysName1' == x.fieldName) {
            x.initialValue = obj[0].sysName1; 
        }
        if ('sysCode2' == x.fieldName) {
            x.initialValue = obj[0].sysCode2; 
        }
        if ('sysName2' == x.fieldName) {
            x.initialValue = obj[0].sysName2; 
        }
        if ('sysCode3' == x.fieldName) {
            x.initialValue = obj[0].sysCode3; 
        }
        if ('sysName3' == x.fieldName) {
            x.initialValue = obj[0].sysName3; 
        }
        if ('sysCode4' == x.fieldName) {
            x.initialValue = obj[0].sysCode4; 
        }
        if ('sysName4' == x.fieldName) {
            x.initialValue = obj[0].sysName4; 
        }
      } );
      this.guzhangxianxiangdaima = false;
    },
    selectfaultCode() {
      this.guzhangxianxiangdaima = true;
    },
    //主损件
    zhusunjianClose() {
      this.zhusunjian = false;
    },
    zhusunjianSave(obj) {
        this.partId = obj[0].partId;
        this.topFilterInit.map(x => {
          if ('partCode' == x.fieldName) {
            x.initialValue = obj[0].partCode; //主损件返显
          }
          if ('partName' == x.fieldName) {
            x.initialValue = obj[0].partName; //主损件返显
          }

        });

        this.zhusunjian = false;
       // this.fetchParams = { ...this.fetchParams };
    },
    selectpartCode() {
        this.zhusunjian = true;
    },
    //搜索数据
    topFilterInitFun() {
      return [
        {
            type: 'INPUT',
            label: '主损件代码',
            fieldName: 'partCode',
            initialValue: '',
            placeholder: '请输入主损件代码...',
            unitRender: p => {
              return (
                <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.selectpartCode()} />
              </div>
            );
            }
          },
          {
            type: 'INPUT',
            label: '主损件名称',
            fieldName: 'partName',
            initialValue: '',
            placeholder: '请输入数量...'
          },
        {
          type: 'INPUT',
          label: '故障现象代码',
          fieldName: 'faultCode',
          readonly: false,
          //rules: [{ required: true, message: '请选择故障现象代码', trigger: 'blur' }],
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.selectfaultCode()} />
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '故障现象描述',
          fieldName: 'faultName',
          placeholder: '请输入...',
          initialValue: undefined,
          disabled: false,
          //rules: [{ required: true, message: '请输入故障现象描述', trigger: 'blur' }]
        },
        { type: 'INPUT', label: '一级代码', fieldName: 'sysCode1', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '一级名称', fieldName: 'sysName1', placeholder: '请输入', initialValue: '',},
        { type: 'INPUT', label: '二级代码', fieldName: 'sysCode2', placeholder: '请输入', initialValue: '', },
        { type: 'INPUT', label: '二级名称', fieldName: 'sysName2', placeholder: '请输入', initialValue: '' ,},
        { type: 'INPUT', label: '三级代码', fieldName: 'sysCode3', placeholder: '请输入', initialValue: '',},
        { type: 'INPUT', label: '三级名称', fieldName: 'sysName3', placeholder: '请输入', initialValue: '',},
        { type: 'INPUT', label: '故障代码', fieldName: 'sysCode4', placeholder: '请输入', initialValue: '' ,},
        { type: 'INPUT', label: '故障名称', fieldName: 'sysName4', placeholder: '请输入', initialValue: '' ,}

      ];
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = Object.assign({}, this.fetchParams,{xhrAbort: false},val);
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 80,
          render: (props, h) => {
            return (
              <div style="text-align:center">
                <multiuse-button size="mini" type="text" onClick={() => this.emitFun(props.row)}>
                  编辑
                </multiuse-button>
                <multiuse-button size="mini" type="text" onClick={() => this.delFun(props.row)}>
                  删除
                </multiuse-button>
              </div>
            );
          }
        },
        { title: '状态', dataIndex: 'statusStr', sorter: true },
        { title: '主损件代码', dataIndex: 'partCode', sorter: true },
        { title: '主损件名称', dataIndex: 'partName', sorter: true },
        { title: '故障现象代码', dataIndex: 'faultCode', sorter: true },
        { title: '故障现象描述', dataIndex: 'faultName', sorter: true },
        { title: '一级代码', dataIndex: 'sysCode1', sorter: true },
        { title: '一级名称', dataIndex: 'sysName1', sorter: true },
        { title: '二级代码', dataIndex: 'sysCode2', sorter: true },
        { title: '二级名称', dataIndex: 'sysName2', sorter: true },
        { title: '三级代码', dataIndex: 'sysCode3', sorter: true },
        { title: '三级名称', dataIndex: 'sysName3', sorter: true },
        { title: '故障代码', dataIndex: 'sysCode4', sorter: true },
        { title: '故障名称', dataIndex: 'sysName4', sorter: true },
      ];
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      this.disableds = this.selectedRows.length <= 0;
      console.log(this.selectedRows);
    },
    //保存
    saveDialog() {
      this.$emit('save', this.selectedRows);
    },
    //取消操作
    closeDialog() {
      this.$emit('close', { visible: false, data: this.selectedRows, type: 'close' });
    },
    chooseOrderNo() {
      this.visible = true;
    }
  }
  // mounted() {
  //   this.BaseTable = this.$refs.listModal.$refs.pageTable;
  // }
};
</script>

<style lang="less" scoped></style>
